/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <memory>
#include <iostream>
#include <baja/db/db_interfaces.h>
#include <baja/error.hpp>

namespace baja { namespace storage {

template <class T>
class cell_typed : public icell_typed<T>
{
public:
    cell_typed(std::weak_ptr<icolumn_typed<T>> column, T value) :
        _column(column),
        _value(value)
    {}
    static std::shared_ptr<cell_typed<T>> create(std::weak_ptr<icolumn_typed<T>> column, T value)
    {
        return std::make_shared<cell_typed<T>>(column, value);
    }
    static std::shared_ptr<cell_typed<T>> createByMoving(std::weak_ptr<icolumn_typed<T>> column, T& value)
    {
        BAJA_THROW(std::logic_error("cell_typed::createByMoving() is not supported for this type."));
        return nullptr;
    }

    // icell
    size_t getSize()
    {
        return sizeof(T);
    }
    column_id getColumnId()
    {
        return _column.lock()->getId();
    }
    std::shared_ptr<icolumn> getColumn()
    {
        return _column.lock();
    }
    void* getBuffer()
    {
        return &_value;
    }
    void dump()
    {
    }

    // icell_typed
    void setValue(T value)
    {
        _value = value;
    }
    void setValueByMoving(T& value)
    {
        BAJA_THROW(std::logic_error("cell_typed::setValueByMoving() is not supported for this type."));
    }
    void getValueByMoving(T& value)
    {
        BAJA_THROW(std::logic_error("cell_typed::getValueByMoving() is not supported for this type."));
    }
    T getValue()
    {
        return _value;
    }
    std::shared_ptr<icolumn_typed<T>> getTypedColumn()
    {
        return _column.lock();
    }
    bool getAppendOnUpdate()
    {
        return false;
    }
    void setAppendOnUpdate(bool append)
    {
        BAJA_THROW(std::logic_error("cell_typed::setAppendOnUpdate() is not supported for this type."));
    }
private:
    std::weak_ptr<icolumn_typed<T>> _column;
    T _value;
};

template <>
class cell_typed <std::wstring> : public icell_typed<std::wstring>
{
public:
    cell_typed(std::weak_ptr<icolumn_typed<std::wstring>> column, std::wstring value) :
        _column(column),
        _value(value)
    {}

    cell_typed(std::weak_ptr<icolumn_typed<std::wstring>> column) :
        _column(column),
        _value()
    {}
    static std::shared_ptr<cell_typed<std::wstring>> create(std::weak_ptr<icolumn_typed<std::wstring>> column, std::wstring value)
    {
        return std::make_shared<cell_typed<std::wstring>>(column, value);
    }
    static std::shared_ptr<cell_typed<std::wstring>> createByMoving(std::weak_ptr<icolumn_typed<std::wstring>> column, std::wstring& value)
    {
        std::shared_ptr<cell_typed<std::wstring>> returnValue = std::make_shared<cell_typed<std::wstring>>(column);
        returnValue->setValueByMoving(value);
        return returnValue;
    }

    // icell
    size_t getSize()
    {
        return _value.size() * sizeof(wchar_t);
    }
    column_id getColumnId()
    {
        return _column.lock()->getId();
    }
    std::shared_ptr<icolumn> getColumn()
    {
        return _column.lock();
    }
    void* getBuffer()
    {
        return static_cast<void*>(const_cast<wchar_t*>(_value.c_str()));
    }
    void dump()
    {
    }

    // icell_typed
    void setValue(std::wstring value)
    {
        if ((value.size() * sizeof(wchar_t)) > _column.lock()->getMaxBufferSizeInBytes())
        {
            BAJA_THROW(std::logic_error("Incoming string length greater than expected.  DB column size needs to be expanded."));
        }
        _value = value;
    }
    void setValueByMoving(std::wstring& value)
    {
        _value = value;
        value = L"";
    }
    void getValueByMoving(std::wstring& value)
    {
        value = _value;
        _value = L"";
    }
    std::wstring getValue()
    {
        return _value;
    }
    std::shared_ptr<icolumn_typed<std::wstring>> getTypedColumn()
    {
        return _column.lock();
    }
    bool getAppendOnUpdate()
    {
        return false;
    }
    void setAppendOnUpdate(bool append)
    {
        BAJA_THROW(std::logic_error("cell_typed::setAppendOnUpdate() is not supported for this type."));
    }

private:
    std::weak_ptr<icolumn_typed<std::wstring>> _column;
    std::wstring _value;
};

template <>
class cell_typed <binary_blob> : public icell_typed<binary_blob>
{
public:
    cell_typed(std::weak_ptr<icolumn_typed<binary_blob>> column, binary_blob value) :
        _column(column),
        _value(value),
        _append(false)
    {
    }

    cell_typed(std::weak_ptr<icolumn_typed<binary_blob>> column) :
        _column(column)
    {
    }
    static std::shared_ptr<cell_typed<binary_blob>> create(std::weak_ptr<icolumn_typed<binary_blob>> column, binary_blob value)
    {
        return std::make_shared<cell_typed<binary_blob>>(column, value);
    }
    static std::shared_ptr<cell_typed<binary_blob>> createByMoving(std::weak_ptr<icolumn_typed<binary_blob>> column, binary_blob& value)
    {
        std::shared_ptr<cell_typed<binary_blob>> returnValue = std::make_shared<cell_typed<binary_blob>>(column);
        returnValue->setValueByMoving(value);
        return returnValue;
    }

    // icell
    size_t getSize()
    {
        return _value._size;
    }
    column_id getColumnId()
    {
        return _column.lock()->getId();
    }
    std::shared_ptr<icolumn> getColumn()
    {
        return _column.lock();
    }
    void* getBuffer()
    {
        return _value._buffer;
    }
    void dump()
    {
    }

    // icell_typed
    void setValue(binary_blob value)
    {
        if (value._size > _column.lock()->getMaxBufferSizeInBytes())
        {
            BAJA_THROW(std::logic_error("Incoming blob size greater than expected.  DB column size needs to be expanded."));
        }
        _value.moveFrom(value);
    }
    void setValueByMoving(binary_blob& value)
    {
        _value.moveFrom(value);
    }
    void getValueByMoving(binary_blob& value)
    {
        value.moveFrom(_value);
    }
    binary_blob getValue()
    {
        return _value;
    }
    std::shared_ptr<icolumn_typed<binary_blob>> getTypedColumn()
    {
        return _column.lock();
    }
    bool getAppendOnUpdate()
    {
        return _append;
    }
    void setAppendOnUpdate(bool append)
    {
        _append = append;
    }

private:
    std::weak_ptr<icolumn_typed<binary_blob>> _column;
    binary_blob _value;
    bool _append;
};

template <class T>
std::shared_ptr<icell_typed<T>> createCell(std::weak_ptr<icolumn_typed<T>> column, T value)
{
    return cell_typed<T>::create(column, value);
}

template <class T>
std::shared_ptr<icell_typed<T>> createCellByMoving(std::weak_ptr<icolumn_typed<T>> column, T& value)
{
    return cell_typed<T>::createByMoving(column, value);
}

}}// namespace baja::storage